package com.shkmall.goods.service.impl;

import com.alibaba.fastjson.JSON;
import com.shkmall.common.utils.IdWorker;
import com.shkmall.goods.dao.*;
import com.shkmall.goods.dto.GoodsDto;
import com.shkmall.goods.pojo.*;
import com.shkmall.goods.service.SpecService;
import com.shkmall.goods.service.SpuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCrypt;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class SpuServiceImpl implements SpuService {
    @Autowired
    private SpuMapper spuMapper;
    @Autowired
    private CategoryMapper categoryMapper;
    @Autowired
    private BrandMapper brandMapper;
    @Autowired
    private SkuMapper skuMapper;
    @Autowired
    private CategoryBrandMapper categoryBrandMapper;
    @Autowired
    private IdWorker idWorker;
        /*添加商品*/
    @Override
    @Transactional
    public void add(GoodsDto goodsDto) {
        Spu spu = goodsDto.getSpu();
        /*添加产品*/
        /*设置默认值*/
        spu.setId(String.valueOf(idWorker.nextId()));
        spu.setSaleNum(0);
        spu.setCommentNum(0);
        spu.setIsMarketable("1");
        spu.setIsEnableSpec("1");
        spu.setStatus("0");
        spu.setIsDelete("0");
        spuMapper.insertSelective(spu);
        /*添加商品*/
        this.addSku(goodsDto);

}
    /*查询spu和 sku根据id*/
    @Override
    public GoodsDto findById(Integer id) {
        Spu spu = spuMapper.selectByPrimaryKey(id);
        Example example = new Example(Sku.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("spuId", id);
        List<Sku> skus = skuMapper.selectByExample(example);
        GoodsDto goodsDto = new GoodsDto();
        goodsDto.setSpu(spu);
        goodsDto.setSkuList(skus);
        return goodsDto;
    }
/*修改商品*/
    @Override
    public void update(String id, GoodsDto goodsDto) {
        Spu spu = goodsDto.getSpu();
        spu.setId(id);
        spuMapper.updateByPrimaryKeySelective(spu);
        List<Sku> skuList = goodsDto.getSkuList();
        if(!skuList.isEmpty()){
            for(Sku sku :skuList){
                /*先删除再添加*/
                Spu key = spuMapper.selectByPrimaryKey(id);
                skuMapper.deleteByPrimaryKey(key);
                this.addSku(goodsDto);
            }
        }
    }


    /*审核*/
    @Override
    public void review(String id) {
        Spu spu = spuMapper.selectByPrimaryKey(id);
        if(spu.getIsDelete().equals("1")){
            throw new RuntimeException("该商品已被删除，无法审核");
        }
        if(spu.getStatus().equals("1")){
            throw new RuntimeException("该商品已审核通过，请不要重复审核");
        }
        if(spu.getStatus().equals("0")){
            spu.setStatus("1");
        }
        spuMapper.updateByPrimaryKeySelective(spu);
    }
        /*上架*/
    @Override
    public void put(String id) {
        Spu spu = spuMapper.selectByPrimaryKey(id);
        if(spu.getStatus().equals("0")){
            throw new RuntimeException("请先对商品进行审核");
        }
        if(spu.getIsDelete().equals("1")){
            throw new RuntimeException("该商品已被删除");
        }
        if(spu.getIsMarketable().equals("0")){
            spu.setIsMarketable("1");
        }
        spuMapper.updateByPrimaryKeySelective(spu);
    }

    /*下架*/
    @Override
    public void off(String id) {
        Spu spu = spuMapper.selectByPrimaryKey(id);
        if(spu.getIsDelete().equals("1")){
            throw new RuntimeException("该商品已被删除");
        }
        if(spu.getIsMarketable().equals("1")){
            spu.setIsMarketable("0");
        }
        spuMapper.updateByPrimaryKeySelective(spu);

    }
/*逻辑删除*/
    @Override
    public void deleteLogic(String id) {
        Spu spu = spuMapper.selectByPrimaryKey(id);
        if(spu.getIsDelete().equals("1")){
            throw new RuntimeException("该商品已被删除");
        }
        if(spu.getIsDelete().equals("0")){
            spu.setIsDelete("1");
        }
        spuMapper.updateByPrimaryKeySelective(spu);
    }
/*还原*/
@Override
public void reduction(String id) {
    Spu spu = spuMapper.selectByPrimaryKey(id);
    if(spu.getIsDelete().equals("0")){
        throw new RuntimeException("该商品无需还原");
    }
    if(spu.getIsDelete().equals("1")){
        spu.setIsDelete("0");
    }
    spuMapper.updateByPrimaryKeySelective(spu);
}

/*物理删除*/
@Override
public void delete(String id) {
    Spu spu = spuMapper.selectByPrimaryKey(id);
    if(!spu.getIsDelete().equals("1")){
        throw new RuntimeException("该商品未被删除");
    }else {
        spuMapper.delete(spu);
    }




}



    public void addSku(GoodsDto goodsDto){
        Spu spu = goodsDto.getSpu();
        List<Sku> skuList = goodsDto.getSkuList();
       Category category= categoryMapper.selectByPrimaryKey(spu.getCategory3Id());

        Brand brand = brandMapper.selectByPrimaryKey(spu.getBrandId());
        if(skuList != null){
            for(Sku sku:skuList){
                /*设置默认值*/
                sku.setId(String.valueOf(idWorker.nextId()));
                String name = spu.getName();
                if(sku.getSpec() == null){
                    sku.setSpec("{}");
                }
                Map<String ,String> map = JSON.parseObject(sku.getSpec(), Map.class);

                for(String spec:map.values()){
                   name+=" "+spec;
                }
                /*设置品牌和分类关系*/
                CategoryBrand categoryBrand = new CategoryBrand();
                categoryBrand.setBrandId(spu.getBrandId());
                categoryBrand.setCategoryId(spu.getCategory3Id());

                int count = categoryBrandMapper.selectCount(categoryBrand);
                if(count == 0){
                    categoryBrandMapper.insertSelective(categoryBrand);
                }

                sku.setName(name);
                sku.setCreateTime(new Date());
                sku.setUpdateTime(new Date());
                sku.setCategoryId(category.getId());
                sku.setCategoryName(category.getName());
                sku.setStatus("1");
                sku.setCommentNum(0);
                sku.setSaleNum(0);
                sku.setBrandName(brand.getName());
                sku.setSpuId(spu.getId());
                skuMapper.insertSelective(sku);
            }
        }
    }
}
